package vaccine.appointment.system.family.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vaccine.appointment.system.common.annotation.AutoWrapResult;
import vaccine.appointment.system.common.annotation.RequireRole;
import vaccine.appointment.system.family.service.FamilyService;
import vaccine.appointment.system.family.vo.AddFamilyRequest;
import vaccine.appointment.system.family.vo.FamilyInfoVO;
import vaccine.appointment.system.user.enums.IdentityInfoEnum;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 家庭控制器
 */
@AutoWrapResult
@RequestMapping("/family")
@RestController
@RequireRole(IdentityInfoEnum.NORMAL_USER)
public class FamilyController {
    @Autowired
    private FamilyService familyService;

    /**
     * 添加家属
     * @param request 添加家属请求
     * @param httpRequest HTTP请求
     * @return 是否成功
     */
    @PostMapping
    public Boolean addFamily(@Valid @RequestBody AddFamilyRequest request, HttpServletRequest httpRequest) {
        Integer userId = (Integer) httpRequest.getAttribute("userId");
        return familyService.addFamily(userId, request);
    }

    /**
     * 查询家属列表
     * @param httpRequest HTTP请求
     * @return 家属列表
     */
    @GetMapping("/list")
    public List<FamilyInfoVO> getFamilyList(HttpServletRequest httpRequest) {
        Integer userId = (Integer) httpRequest.getAttribute("userId");
        return familyService.getFamilyList(userId);
    }

    /**
     * 删除家属
     * @param id 家属关系ID
     * @param httpRequest HTTP请求
     * @return 是否成功
     */
    @DeleteMapping("/{id}")
    public Boolean deleteFamily(@PathVariable Integer id, HttpServletRequest httpRequest) {
        Integer userId = (Integer) httpRequest.getAttribute("userId");
        return familyService.deleteFamily(userId, id);
    }
}
